package vn.edu.fpt.xml.back.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import java.sql.Timestamp;
import java.util.Set;

/**
 * Created by hiephn on 2014/04/24.
 */
@Entity
@Table(name = "article")
@XmlRootElement
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ArticleEntity extends JpaEntity {
    private String title;
    private String description;
    private String image;
    private Timestamp publicDate;
    private String originUrl;
    private boolean onHomePage;
    private String channelId;
    private ChannelEntity channelByChannelId;
    private String content;
    private String channelTitle;
    private String publicDateString;

    @Transient
    @XmlElement
    public String getPublicDateString() {
        return publicDateString;
    }

    public void setPublicDateString(String publicDateString) {
        this.publicDateString = publicDateString;
    }

    @Transient
    public String getChannelTitle() {
        return channelTitle;
    }

    public void setChannelTitle(String channelTitle) {
        this.channelTitle = channelTitle;
    }

    @Basic
    @Column(name = "content")
    @XmlElement
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Basic
    @Column(name = "title")
    @XmlElement
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Basic
    @Column(name = "description")
    @XmlElement
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Basic
    @Column(name = "image")
    @XmlElement
    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    @Basic
    @Column(name = "publicDate")
    @XmlTransient
    @JsonIgnore
    public Timestamp getPublicDate() {
        return publicDate;
    }

    public void setPublicDate(Timestamp publicDate) {
        this.publicDate = publicDate;
    }

    @Basic
    @Column(name = "originUrl")
    @XmlElement
    public String getOriginUrl() {
        return originUrl;
    }

    public void setOriginUrl(String originUrl) {
        this.originUrl = originUrl;
    }

    @Basic
    @Column(name = "onHomePage")
    @JsonIgnore
    public boolean isOnHomePage() {
        return onHomePage;
    }

    public void setOnHomePage(boolean onHomePage) {
        this.onHomePage = onHomePage;
    }

    @Basic
    @Column(name = "channelId", insertable = false, updatable = false)
    @XmlTransient
    @JsonIgnore
    public String getChannelId() {
        return channelId;
    }

    public void setChannelId(String channelId) {
        this.channelId = channelId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ArticleEntity that = (ArticleEntity) o;

        if (channelId != that.channelId) return false;
        if (onHomePage != that.onHomePage) return false;
        if (description != null ? !description.equals(that.description) : that.description != null) return false;
        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (image != null ? !image.equals(that.image) : that.image != null) return false;
        if (originUrl != null ? !originUrl.equals(that.originUrl) : that.originUrl != null) return false;
        if (publicDate != null ? !publicDate.equals(that.publicDate) : that.publicDate != null) return false;
        if (title != null ? !title.equals(that.title) : that.title != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (title != null ? title.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (image != null ? image.hashCode() : 0);
        result = 31 * result + (publicDate != null ? publicDate.hashCode() : 0);
        result = 31 * result + (originUrl != null ? originUrl.hashCode() : 0);
        result = 31 * result + (onHomePage ? 1 : 0);
        result = 31 * result + (channelId != null ? channelId.hashCode() : 0);
        return result;
    }

    @ManyToOne
    @JoinColumn(name = "channelId", referencedColumnName = "id", nullable = false)
    @XmlTransient
    @JsonIgnore
    public ChannelEntity getChannelByChannelId() {
        return channelByChannelId;
    }

    public void setChannelByChannelId(ChannelEntity channelByChannelId) {
        this.channelByChannelId = channelByChannelId;
    }
}
